package com.cms.work.core;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.github.pagehelper.PageInfo;

/**
 * <p>Title: PageInfoUtil</p>
 * <p>Description: 将查询请求的参数封装到PageInfo对象中,实现分页查询功能</p>
 * <p>Email: yuyue_qty@163.com</p>
 * @author 晴天雨
 * @date 2016年8月23日下午11:55:01
 * @version 1.0
 */
public class PageInfoUtil<T> {
    
    public PageInfo<T> getPageInfo(HttpServletRequest request, T t) throws Exception {
        PageInfo<T> pageInfo = new PageInfo<T>();
        
        //表列数
        String isSortingCols = request.getParameter("order[0][column]");
        //排序(desc、asc)
        String dir = request.getParameter("order[0][dir]");
        //排序字段名
        String orderBy = request.getParameter("columns["+ isSortingCols +"][data]");
        
        if(null != isSortingCols && null != orderBy && null != dir){
            pageInfo.setOrderBy(orderBy+" "+dir);
        }
        
        Integer pageNum = (request.getParameter("start")==null?0:Integer.valueOf(request.getParameter("start")));
        Integer pageSize = (request.getParameter("length")==null?10:Integer.valueOf(request.getParameter("length")));
        
        pageInfo.setPageNum(pageNum/pageSize+1);
        pageInfo.setPageSize(pageSize);
        List<T> list = new ArrayList<T>();
        list.add(t);
        pageInfo.setList(list);
        return pageInfo;
    }
    
}
